package com.nuoniu.sibanyun.mapper.finance;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.dto.AccountingAuxiliaryLedgerDto;
import com.nuoniu.sibanyun.entity.dto.AccountingLedgerDto;
import com.nuoniu.sibanyun.entity.finance.VAccountingLedger;
import com.nuoniu.sibanyun.entity.finance.VAccountingSearchProgramme;
import com.nuoniu.sibanyun.entity.finance.VAccountingVoucherDetail;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会计凭证细表 Mapper 接口
 * </p>
 *
 * @author 小啊刚
 * @since 2021-04-09
 */
public interface VAccountingVoucherDetailMapper extends BaseMapper<VAccountingVoucherDetail> {

    @Select("select count(1) from fag_voucher_entry where accounting_id=#{id}")
    Integer getByAccountingId(Long id);

//    @Select("select IFNULL(debit_amount, 0) debit_amount, IFNULL(credit_amount, 0) credit_amount, accounting_id, accounting_code, accounting_name, crm_id, crm_name, user_id, user_name, vendor_id, vendor_name, project_id, project_name, office_id, office_name, account_year, account_period " +
    @Select("select IFNULL(debit_amount, 0) debit_amount, IFNULL(credit_amount, 0) credit_amount, accounting_id, accounting_code, accounting_name, customer_id, customer_name, employee_id, employee_name, vendor_id, vendor_name, project_id, project_name, office_id, office_name, account_year, account_period " +
            "from fag_voucher_entry where order_id in (${accountingIds})")
    List<VAccountingVoucherDetail> getAccountingAmount(String accountingIds);

    @Update("update fag_voucher_entry set status=3 where order_id in (${aIds})")
    void updateStatus(String aIds);

    List<VAccountingLedger> countAccounting(VAccountingSearchProgramme searchProgramme);

    /**
     * 获取总分类账明细列表
     * @author 小啊刚
     * @date 2021-05-11
     */
    List<AccountingLedgerDto> getLedgerDetailList(@Param("dto") VAccountingSearchProgramme searchProgramme);

    List<AccountingAuxiliaryLedgerDto> getAuxiliaryLedgerDetailList(@Param("dto") VAccountingSearchProgramme searchProgramme);

    @Select("select description from fag_voucher_entry where order_id=#{mainId} limit 1")
    VAccountingVoucherDetail getOne(Long mainId);

    @Select("select sum(debit_amount) debitAmount, sum(credit_amount) creditAmount from  fag_voucher_entry where order_id=#{mainId} group by order_id")
    VAccountingVoucherDetail getSumAmount(Long mainId);
}
